<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="client" />
	<meta name="description" content="client" />
	<!-- 网页标签标题 -->
	<title>client</title>
	<link rel="shortcut icon" href="/img/docsite.ico"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/zh-cn/index.html"><img class="logo" src="/img/txlcn.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">En</span><div class="header-menu"><img class="header-menu-toggle" src="/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/zh-cn/index.html" target="_self">首页</a></li><li class="menu-item menu-item-normal"><a href="https://bbs.txlcn.org/viewtopic.php?id=42" target="_self">新闻</a></li><li class="menu-item menu-item-normal"><a href="https://bbs.txlcn.org" target="_self">社区</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/zh-cn/docs/preface.html" target="_self">文档</a></li><li class="menu-item menu-item-normal"><a href="https://github.com/codingapi/tx-lcn/releases" target="_blank">下载</a></li><li class="menu-item menu-item-normal"><a href="/zh-cn/sponsor/index.html" target="_self">赞助</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/system/docs.png" class="front-img"/><span>文档</span><img src="/img/system/docs.png" class="back-img"/></div></div><section class="content-section"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>用户文档</span><ul><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/preface.html" target="_self">入门</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/background.html" target="_self">背景</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/start.html" target="_self">快速开始</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/dependencies.html" target="_self">依赖</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>示例<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/demo/env.html" target="_self">分布式事务示例</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/demo/dubbo.html" target="_self">TC之Dubbo</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/demo/springcloud.html" target="_self">TC之SpringCloud</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>原理介绍<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/principle/control.html" target="_self">控制原理</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/principle/lcn.html" target="_self">LCN模式</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/principle/tcc.html" target="_self">TCC模式</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/principle/txc.html" target="_self">TXC模式</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>配置手册<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/setting/client.html" target="_self">TC配置</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/setting/manager.html" target="_self">TM配置</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/setting/distributed.html" target="_self">集群与负载</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>扩展支持<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/expansion/transaction.html" target="_self">事务模式扩展</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/expansion/message.html" target="_self">通讯协议扩展</a></li><li class="menu-item menu-item-level-3"><a href="/zh-cn/docs/expansion/rpc.html" target="_self">RPC框架扩展</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/communication.html" target="_self">通讯指令手册</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/manageradmin.html" target="_self">TM管理手册</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/debug.html" target="_self">问题排查手册</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/test.html" target="_self">性能测试报告</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/developer.html" target="_self">开发者</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/fqa.html" target="_self">FQA</a></li></ul></li></ul></div><div class="doc-content markdown-body"><h1>TxClient配置说明</h1>
<h2>一、application.properties</h2>
<pre><code class="language-properties"><span class="hljs-comment">
# springcloud feign 下开启负载均衡时的配置。开启后同一个事务组下相同的模块会重复调用。</span>
<span class="hljs-comment"># 对应dubbo框架下需要设置的是 @Reference的loadbalance，有下面四种，作用都是开启后同一个事务组下相同的模块会重复调用。</span>
<span class="hljs-comment">#txlcn_random=com.codingapi.txlcn.tracing.dubbo.TxlcnRandomLoadBalance</span>
<span class="hljs-comment">#txlcn_roundrobin=com.codingapi.txlcn.tracing.dubbo.TxlcnRoundRobinLoadBalance</span>
<span class="hljs-comment">#txlcn_leastactive=com.codingapi.txlcn.tracing.dubbo.TxlcnLeastActiveLoadBalance</span>
<span class="hljs-comment">#txlcn_consistenthash=com.codingapi.txlcn.tracing.dubbo.TxlcnConsistentHashLoadBalance</span>

<span class="hljs-meta">tx-lcn.ribbon.loadbalancer.dtx.enabled</span>=<span class="hljs-string">true</span>
<span class="hljs-comment"># tx-manager 的配置地址，多个用,分割。注意设置上的地址在启动的时候会检查并连接，连接不成功会启动失败。</span>
<span class="hljs-comment"># tx-manager 下集群策略，当增加一个新的tx-manager后，tx-manager也会通知到其他的在线模块，然后tx-client会在连接上后面加入的模块。</span>
<span class="hljs-meta">tx-lcn.client.manager-address</span>=<span class="hljs-string">127.0.0.1:8070,127.0.0.1:8071</span>
<span class="hljs-comment"># 该参数是分布式事务框架存储的业务切面信息。采用的是h2数据库。绝对路径。该参数默认的值为{user.dir}/.txlcn/{application.name}-{application.port}</span>
<span class="hljs-meta">tx-lcn.aspect.log.file-path</span>=<span class="hljs-string">logs/.txlcn/demo-8080</span>
<span class="hljs-comment"># 调用链长度等级，默认值为3.标识调用连长度为3，该参数是用于识别分布式事务的最大通讯时间。</span>
<span class="hljs-meta">tx-lcn.client.chain-level</span>=<span class="hljs-string">3</span>
<span class="hljs-comment"># 该参数为tc与tm通讯时的最大超时时间，单位毫米。该参数不需要配置会在连接初始化时由tm返回。</span>
<span class="hljs-meta">tx-lcn.client.tm-rpc-timeout</span>=<span class="hljs-string">2000</span>
<span class="hljs-comment"># 该参数为分布式事务的最大时间，单位毫米。该参数不需要配置会在连接初始化时由tm返回。</span>
<span class="hljs-meta">tx-lcn.client.dtx-time</span>=<span class="hljs-string">50000</span>
<span class="hljs-comment"># 该参数为雪花算法的机器编号。该参数不需要配置会在连接初始化时由tm返回。</span>
<span class="hljs-meta">tx-lcn.client.machine-id</span>=<span class="hljs-string">1</span>
<span class="hljs-comment">#该参数为事务方法注解切面的orderNumber，默认值为0.</span>
<span class="hljs-meta">tx-lcn.client.dtx-aspect-order</span>=<span class="hljs-string">0</span>
<span class="hljs-comment">#该参数为事务连接资源方法切面的orderNumber，默认值为0.</span>
<span class="hljs-meta">tx-lcn.client.resource-order</span>=<span class="hljs-string">0</span>
<span class="hljs-comment">#是否开启日志记录。当开启以后需要配置对应logger的数据库连接配置信息。</span>
<span class="hljs-meta">tx-lcn.logger.enabled</span>=<span class="hljs-string">false</span>
<span class="hljs-comment">
#该参数为tm下的配置，tc下忽略</span>
<span class="hljs-meta">tx-lcn.client.tx-manager-delay</span>=<span class="hljs-string">2000</span>
<span class="hljs-comment">#该参数为tm下的配置，tc下忽略</span>
<span class="hljs-meta">tx-lcn.client.tx-manager-heart</span>=<span class="hljs-string">2000</span>


</code></pre>
<h2>二、特别配置</h2>
<h3>1、微服务<code>集群</code>且用到 LCN事务模式时，为保证性能请开启TX-LCN重写的负载策略。</h3>
<ul>
<li>Dubbo 开启</li>
</ul>
<pre><code class="language-java"><span class="hljs-meta">@Reference</span>(version = <span class="hljs-string">"${demo.service.version}"</span>,
        application = <span class="hljs-string">"${dubbo.application.e}"</span>,
        retries = -<span class="hljs-number">1</span>,
        registry = <span class="hljs-string">"${dubbo.registry.address}"</span>,
        loadbalance = <span class="hljs-string">"txlcn_random"</span>)  <span class="hljs-comment">// here</span>
<span class="hljs-keyword">private</span> EDemoService eDemoService;
</code></pre>
<ul>
<li>SpringCloud 开启 (application.properties)</li>
</ul>
<pre><code class="language-properties"><span class="hljs-meta">tx-lcn.springcloud.loadbalance.enabled</span>=<span class="hljs-string">true</span>
</code></pre>
<p>配置详情<a href="distributed.html">参见</a></p>
<h3>2、关闭业务RPC重试</h3>
<ul>
<li>Dubbo 开启</li>
</ul>
<pre><code class="language-java"><span class="hljs-meta">@Reference</span>(version = <span class="hljs-string">"${demo.service.version}"</span>,
        application = <span class="hljs-string">"${dubbo.application.e}"</span>,
        retries = -<span class="hljs-number">1</span>,
        registry = <span class="hljs-string">"${dubbo.registry.address}"</span>,
        loadbalance = <span class="hljs-string">"txlcn_random"</span>)  <span class="hljs-comment">// here</span>
<span class="hljs-keyword">private</span> EDemoService eDemoService;
</code></pre>
<ul>
<li>SpringCloud 开启 (application.properties)</li>
</ul>
<pre><code class="language-properties"><span class="hljs-comment"># 关闭Ribbon的重试机制</span>
<span class="hljs-meta">ribbon.MaxAutoRetriesNextServer</span>=<span class="hljs-string">0</span>
</code></pre>
<hr>
<p><code>NOTE</code><br>
1、TxClient所有配置均有默认配置，请按需覆盖默认配置。<br>
2、为什么要关闭服务调用的重试。远程业务调用失败有两种可能：
（1），远程业务执行失败 （2）、远程业务执行成功，网络失败。对于第2种，事务场景下重试会发生，某个业务执行两次的问题。
如果业务上控制某个事务接口的幂等，则不用关闭重试。</p>
<hr>
<h3>3、通过AOP配置本地事务与分布式事务</h3>
<pre><code>@Configuration
@EnableTransactionManagement
public class TransactionConfiguration {

    /**
     * 本地事务配置
     * @param transactionManager
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public TransactionInterceptor transactionInterceptor(PlatformTransactionManager transactionManager) {
        Properties properties = new Properties();
        properties.setProperty(&quot;*&quot;, &quot;PROPAGATION_REQUIRED,-Throwable&quot;);
        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        transactionInterceptor.setTransactionManager(transactionManager);
        transactionInterceptor.setTransactionAttributes(properties);
        return transactionInterceptor;
    }

    /**
     * 分布式事务配置 设置为LCN模式
     * @param dtxLogicWeaver
     * @return
     */
    @ConditionalOnBean(DTXLogicWeaver.class)
    @Bean
    public TxLcnInterceptor txLcnInterceptor(DTXLogicWeaver dtxLogicWeaver) {
        TxLcnInterceptor txLcnInterceptor = new TxLcnInterceptor(dtxLogicWeaver);
        Properties properties = new Properties();
        properties.setProperty(Transactions.DTX_TYPE,Transactions.LCN);
        properties.setProperty(Transactions.DTX_PROPAGATION, &quot;REQUIRED&quot;);
        txLcnInterceptor.setTransactionAttributes(properties);
        return txLcnInterceptor;
    }

    @Bean
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
        //需要调整优先级，分布式事务在前，本地事务在后。
        beanNameAutoProxyCreator.setInterceptorNames(&quot;txLcnInterceptor&quot;,&quot;transactionInterceptor&quot;);
        beanNameAutoProxyCreator.setBeanNames(&quot;*Impl&quot;);
        return beanNameAutoProxyCreator;
    }
}

</code></pre>
<h3>4、TXC模式定义表的实际主键</h3>
<p>TXC 是基于逆向sql的方式实现对业务的回滚控制，在逆向sql操作数据是会检索对应记录的主键作为条件处理回滚业务。但是在有些情况下可能表中并没有主键字段(primary key)，仅存在业务上的名义主键，此时可通过重写<code>PrimaryKeysProvider</code>方式定义表对应的主键关系。</p>
<p>如下所示:</p>
<pre><code class="language-java">
<span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MysqlPrimaryKeysProvider</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PrimaryKeysProvider</span> </span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> Map&lt;String, List&lt;String&gt;&gt; provide() {
        <span class="hljs-comment">//t_demo 表的回滚主键为 kid字段</span>
        <span class="hljs-keyword">return</span> Maps.newHashMap(<span class="hljs-string">"t_demo"</span>, Collections.singletonList(<span class="hljs-string">"kid"</span>));
    }
}

</code></pre>
</div></section><footer class="footer-container"><div class="footer-body"><img src="/img/txlcn.png"/><div class="cols-container"><div class="col col-12"><h3>愿景</h3><p>我们将致力于打造一个快捷、高效、兼任性强的分布式事务解决方案</p></div><div class="col col-6"><dl><dt>导航</dt><dd><a href="/zh-cn/docs/txlcn-arch.html" target="_self">TX-LCN解决方案</a></dd><dd><a href="/zh-cn/docs/start.html" target="_self">快速上手</a></dd><dd><a href="/zh-cn/docs/txlcn-mod.html" target="_self">开发计划</a></dd><dd><a href="https://bbs.txlcn.org/viewforum.php?id=8" target="_self">报告Bug</a></dd></dl></div><div class="col col-6"><dl><dt>友情链接</dt><dd><a href="https://www.codingapi.com/" target="_blank">CodingApi</a></dd></dl></div></div><div class="copyright"><span>Copyright © 2018-2019 CodingApi</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>